#include "ctsocketrunnable.h"
#include "ctclientsocket.h"
#include "global.h"

CTSocketRunnable::CTSocketRunnable(QVector<SocketDnInfo> dnVects)
{
    m_dnVects= dnVects;

}
CTSocketRunnable::CTSocketRunnable(QMap<QString, DomainQryAddrInfo> dnMaps)
{
    QMap<QString, DomainQryAddrInfo>::const_iterator iter;

    for (iter = dnMaps.constBegin(); iter != dnMaps.constEnd(); ++iter)
    {
        std::unique_ptr<CTClientSocket> clientSocket(new CTClientSocket()); // 使用 std::unique_ptr 替换 QScopedPointer
        //CTClientSocket *clientSocket = new CTClientSocket();
        DomainQryAddrInfo dqai = iter.value();

        clientSocket->setServerAddress(dqai.qryAddress);
        clientSocket->setServerPort(dqai.port.toInt());
        clientSocket->setSendMsg(iter.key());
        clientSocket->setMsgFlag(iter.key(), iter.key());

        m_clientSockets.push_back(std::move(clientSocket)); // 使用 std::move 移动智能指针到容器中
    }
}
CTSocketRunnable::~CTSocketRunnable()
{
    qDebug()<<"~CTSocketRunnable()";
}
void CTSocketRunnable::run()
{
//    QMutexLocker locker(&g_dnQueueMutex); // 假设 g_dnQueueMutex 为保护 g_dnQueue 的互斥锁

//    g_dnQueue.clear();

//    foreach(const auto & dn, m_dnVects) {
//        auto clientSocket = std::make_unique<CTClientSocket>(); // 使用智能指针管理生命周期

//        clientSocket->setServerAddress(dn.address);
//        clientSocket->setServerPort(dn.port.toInt());
//        clientSocket->setSendMsg(dn.msg);
//        clientSocket->setRequestParam(dn.reqParam);

//        g_dnQueue.enqueue(&clientSocket);

//        connect(clientSocket.get(), &CTClientSocket::dataSocketReceived, this,
//                [&](QByteArray &data, QString &requestParam){
//                    dataReceivedFromSocket(data, requestParam);
//                });

//        // 直接启动 clientSocket，无需先移动到线程
//        QThreadPool::globalInstance()->start(clientSocket.get());
//    }

    foreach(const auto & dn, m_dnVects) {
        auto clientSocket = new CTClientSocket();
        clientSocket->setServerAddress(dn.address);
        clientSocket->setServerPort(dn.port.toInt());
        clientSocket->setSendMsg(dn.msg);
        clientSocket->setRequestParam(dn.reqParam);
        clientSocket->moveToThread(QThreadPool::globalInstance()->thread());
        QMetaObject::invokeMethod(clientSocket, "startConnection");
        //QThreadPool::globalInstance()->start(clientSocket);
        QEventLoop loop;
        connect(clientSocket, &CTClientSocket::dataSocketReceived, &loop, &QEventLoop::quit);
        connect(clientSocket, &CTClientSocket::dataSocketReceived, this,
                [&](QByteArray data, QString requestParam){
                    dataReceivedFromSocket(data, requestParam);

                });
        loop.exec();

        //clientSocket->moveToThread(QThreadPool::globalInstance()->thread());
        //qDebug()<<"QEventLoop";
    }


//    foreach(const auto & dn, m_dnVects) {
//        std::unique_ptr<CTClientSocket> clientSocket(new CTClientSocket());
//        clientSocket->setServerAddress(dn.address);
//        clientSocket->setServerPort(dn.port.toInt());
//        clientSocket->setSendMsg(dn.msg);
//        clientSocket->setRequestParam(dn.reqParam);
//        connect(clientSocket.get(), &CTClientSocket::dataSocketReceived, this, [&](QByteArray &data, QString &requestParam){dataReceivedFromSocket(data,requestParam);});
//        clientSocket->moveToThread(QThreadPool::globalInstance()->thread());
//        clientSocket->startConnection();
//    }
//    for (auto& clientSocket : m_clientSockets)
//    {
//        qDebug()<<"send msg"<<this<<clientSocket.get()<<clientSocket->getRequestParam();
//        clientSocket->startConnection();
//        connect(clientSocket.get(), &CTClientSocket::dataSocketReceived, this, [&](QByteArray &data, QString &receivedRequestId)
//                {dataReceivedFromSocket(data,receivedRequestId);});

//        QThread::msleep(5000);
//    }
}

void CTSocketRunnable::startClientSockets()
{
    for (auto& clientSocket : m_clientSockets)
    {
//        clientSocket->startConnection();
//        connect(clientSocket.get(), &CTClientSocket::dataSocketReceived, this, [&](QByteArray &data, QString &requestParam)
//                {dataReceivedFromSocket(data,requestParam);});

//        qDebug()<<"send msg"<<this<<clientSocket.get()<<m_requestId;

        //QThread::msleep(500);
    }
}

void CTSocketRunnable::dataReceivedFromSocket(const QByteArray data, const QString requestParam)
{
    qDebug()<<"dataReceivedFromSocket"<<requestParam<<data.length();
        emit resultReady(data,requestParam);
}


void CTSocketRunnable::stopClientSockets()
{
//    for (auto& clientSocket : m_clientSockets)
//    {
//        clientSocket->disconnectFromHost();
//        // ... 其他清理操作，如断开信号槽连接
//    }
}
